#include "i2c.h"

#include "twi_master.h"
#include "spi_master.h"
#include "nrf51_bitfields.h"
#include "app_util_platform.h"
#include "boards.h"

#define TX_RX_BUF_LENGTH         16u                 /**< SPI transaction buffer length. */

#if defined(SPI_MASTER_0_ENABLE)
    #define SPI_MASTER_HW SPI_MASTER_0
#elif defined(SPI_MASTER_1_ENABLE)
    #define SPI_MASTER_HW SPI_MASTER_1
#else
    #error "No SPI enabled"
#endif

// Data buffers.
//static uint8_t m_tx_data[TX_RX_BUF_LENGTH] = {0}; /**< A buffer with data to transfer. */
//static uint8_t m_rx_data[TX_RX_BUF_LENGTH] = {0}; /**< A buffer for incoming data. */
//static volatile bool m_transfer_completed = true; /**< A flag to inform about completed transfer. */

static uint32_t spi_master_init(void)
{
    spi_master_config_t spi_config = SPI_MASTER_INIT_DEFAULT;
    
    #if defined(SPI_MASTER_0_ENABLE)
    spi_config.SPI_Pin_SCK  = SPIM0_SCK_PIN;
    spi_config.SPI_Pin_MISO = SPIM0_MISO_PIN;
    spi_config.SPI_Pin_MOSI = SPIM0_MOSI_PIN;
    spi_config.SPI_Pin_SS   = SPIM0_SS_PIN;
    #elif defined(SPI_MASTER_1_ENABLE)
    spi_config.SPI_Pin_SCK  = SPIM1_SCK_PIN;
    spi_config.SPI_Pin_MISO = SPIM1_MISO_PIN;
    spi_config.SPI_Pin_MOSI = SPIM1_MOSI_PIN;
    spi_config.SPI_Pin_SS   = SPIM1_SS_PIN;
    #endif /* SPI_MASTER_ENABLE */

    return spi_master_open(SPI_MASTER_HW, &spi_config);
}

static void spi_master_event_handler(spi_master_evt_t spi_master_evt)
{
    uint32_t err_code = NRF_SUCCESS;
    bool result = false;

    switch (spi_master_evt.evt_type)
    {
        case SPI_MASTER_EVT_TRANSFER_COMPLETED:

            // Check if data are vaild.
//            result = buf_check(m_rx_data, spi_master_evt.data_count);
//            APP_ERROR_CHECK_BOOL(result);

//            // Inform application that transfer is completed.
//            m_transfer_completed = true;
            break;

        default:
            // No implementation needed.
            break;
    }
}



void i2c_init(void)
{
//    twi_master_init();
    
    spi_master_init();

}

int Sensors_I2C_ReadRegister(uint8_t slaveAddr, uint8_t readAddr, uint16_t NumByteToRead, uint8_t* pBuffer)
{
//    int transfer_succeeded;
//    transfer_succeeded  = twi_master_transfer(slaveAddr, &readAddr, 1, TWI_DONT_ISSUE_STOP);
//    transfer_succeeded &= twi_master_transfer(slaveAddr|TWI_READ_BIT, (uint8_t *)pBuffer, NumByteToRead, TWI_ISSUE_STOP);
//    
//    if(transfer_succeeded)
//        return 0;
//    else
//        return -1;
    uint8_t m_tx_data[2];
    
    //m_tx_data[0] = slaveAddr | 0x80;
    m_tx_data[0] = readAddr | 0x80;
    
    uint8_t dummy_data = 0x00;
    
    nrf_gpio_pin_clear(4);
    
    uint32_t err_code = spi_master_send_recv(SPI_MASTER_HW, m_tx_data, 1, NULL, 0);
    
//    for(int i = 0; i < NumByteToRead; i++)
//    {
        err_code = spi_master_send_recv(SPI_MASTER_HW, &dummy_data, NumByteToRead, pBuffer, NumByteToRead);
//    }
    
    nrf_gpio_pin_set(4);
    
    return err_code;

}

int Sensors_I2C_WriteRegister(uint8_t slaveAddr, uint8_t writeAddr, uint16_t RegisterLen, const uint8_t *pBuffer)
{
//    uint8_t write_Buf[20];
//    uint8_t i;
//    int transfer_succeeded;
//    
//    write_Buf[0] = writeAddr;
//    for(i = 0; i < RegisterLen; i++)
//    {
//        write_Buf[i+1] = pBuffer[i];
//    }
//    
//    transfer_succeeded  = twi_master_transfer(slaveAddr, write_Buf, RegisterLen+1, TWI_ISSUE_STOP);
//    
//    if(transfer_succeeded)
//        return 0;
//    else
//        return -1;
    
    uint8_t write_Buf[20];
    uint8_t i;
    
//    write_Buf[0] = slaveAddr;
    write_Buf[0] = writeAddr;
    
    nrf_gpio_pin_clear(4);
    
    for(i = 0; i < RegisterLen; i++)
    {
        write_Buf[i+1] = pBuffer[i];
    }
    
    uint32_t err_code = spi_master_send_recv(SPI_MASTER_HW, write_Buf, RegisterLen+1, NULL, 0);
    
    nrf_gpio_pin_set(4);
    
    return err_code;
}


